mod config;
mod data;
mod endpoint;
mod client;
mod stats;
mod task;
mod report;

// main.rs
use crate::config::config_reader::Config;
use crate::data::excel_reader::TestData;
use crate::endpoint::endpoint_reader::Endpoint;
use crate::task::performance_task::run_performance_test;
use crate::stats::stats_collector::Stats;
use crate::report::report_generator::generate_report;
use std::sync::{Arc, Mutex};
use tokio;

#[tokio::main]
async fn main() {
    // 1. 读取配置文件
    let config = Config::from_excel("data/config.xlsx").expect("Failed to read config");

    // 2. 读取接口配置
    let endpoints = Endpoint::from_excel(&config.endpoints_file)
        .expect("Failed to read endpoints");

    // 3. 读取测试数据
    let test_data = TestData::from_excel(&config.data_file)
        .expect("Failed to read test data");

    // 4. 初始化统计信息
    let stats = Arc::new(Mutex::new(Stats::new()));

    // 5. 执行性能测试任务
    run_performance_test(
        &config.url,
        endpoints.iter().map(|e| e.path.clone()).collect(),
        config.total_requests,
        config.concurrency,
        config.request_interval,
        config.timeout,
        stats.clone(),
    )
        .await;

    // 6. 生成并输出报告
    let stats = stats.lock().unwrap();
    generate_report(&stats).expect("Failed to generate report");

    println!("Test complete! Report generated.");
}